package cn.zwx.business.member.service.remote;

import cn.zwx.business.order.api.req.RpcRequest;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @author zhangwenxue
 */
public class RpcNetTransport {

    private String host;

    private int port;

    public RpcNetTransport(String host, int port) {
        this.host = host;
        this.port = port;
    }


    public Socket newSocket()  {
        Socket socket= null;
        try {
            socket = new Socket(host,port);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return socket;
    }


    public Object send(RpcRequest request){
        // 构建客户端
        Socket socket = newSocket();
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());){
             // 写出去
             objectOutputStream.writeObject(request);
             objectOutputStream.flush();
             // 读进来 
            return inputStream.readObject();
        }catch (Exception e){
           e.printStackTrace();
        }
        return null;
    }
}
